﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace gbk_utf8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
      
       
       
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog();
            if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                DirectoryInfo dir = new DirectoryInfo(folder.SelectedPath);
                FindFile(dir);
                
            }
        }
        /// <summary>
        /// 便利文件
        /// </summary>
        /// <param name="info"></param>
       
        public void FindFile(DirectoryInfo dir)   //参数为指定的目录  
        {
            foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
            {
                if (fsi is FileInfo)
                {
                    FileInfo fi = (FileInfo)fsi;
                    if (AFilter(fi.FullName))
                    {
                        ListViewItem item = new ListViewItem(new string[]{
                        fi.Name,fi.FullName
                    });
                        listView1.Items.Add(item);
                    }
                   
                }
                else
                {
                    DirectoryInfo di = (DirectoryInfo)fsi;
                    FindFile(di);
                }
            }
        }

        public Boolean AFilter(string path)
        {
            Boolean temp = false;
            string Suffix=System.IO.Path.GetExtension(path);
            string[] value = new string[] { 
                ".txt",".html",".sql",".html",".js",".css",".php",".htm"
            };
            foreach (string item in value)
            {
                if (item == Suffix)
                {
                    temp = true;
                }
            }
            return temp;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                toolStripProgressBar1.Maximum = listView1.Items.Count;
                toolStripProgressBar1.Minimum = 0;
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    ///GB2312转换UTF8
                    string path = listView1.Items[i].SubItems[1].Text.ToString();
                    var data = File.ReadAllBytes(path);
                    data = Encoding.UTF8.GetBytes(Encoding.GetEncoding("gb2312").GetString(data));
                    File.WriteAllBytes(path, data);
                    toolStripProgressBar1.Value = i + 1;
                }
            }
            else
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    ///UTF8转换GB2312
                    string path = listView1.Items[i].SubItems[1].Text.ToString();
                    var data = File.ReadAllBytes(path);
                    data = Encoding.GetEncoding("gb2312").GetBytes(Encoding.UTF8.GetString(data));
                    File.WriteAllBytes(path, data);
                    toolStripProgressBar1.Value = i + 1;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.ShowDialog();
        }






    



    }
}
